package com.topisv.tms.web.stock.movestorage;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.topisv.tms.entity.stock.CusStock;
import com.topisv.tms.service.stock.CusStockService;
import com.topisv.tms.web.base.BaseController;

@Controller
@RequestMapping(value = "/v2_faultymoveStorage")
public class FaultyMoveStorageController extends BaseController {

	@Autowired
	private CusStockService cusStockService;

	@RequestMapping(value = "show")
	public String show(Model model) {
		fillSelectValue(model, null);
		return "stock/movestorage/faultyMoveStorage";
	}

	@RequestMapping(value = "query",method=RequestMethod.POST)
	public String query(Model model, 
			@RequestParam(value = "warehouseCode") String warehouseCode,
			@RequestParam(value = "customerCode") String customerCode,
			@RequestParam(value = "sku") String sku) {
		List<CusStock> cusStocks = cusStockService.loadStock(warehouseCode, customerCode, sku);
		model.addAttribute("cusStocks", cusStocks);
		fillSelectValue(model, customerCode);
		return "stock/movestorage/faultyMoveStorage";
	}
	@RequestMapping(value = "move",method=RequestMethod.POST)
	@ResponseBody
	public String move(@RequestBody CusStock[] cusStocks){
		CusStock from = null;
		CusStock to = null;
		for (CusStock cusStock : cusStocks) {
			if("from".equals(cusStock.getMoveFlg())){
				from = cusStock;
			}else{
				to = cusStock;
			}
		}
		return cusStockService.faultyMove(from, to);
	}

}
